package com.chinaums.xm.wechat.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.chinaums.xm.wechat.domain.wechat.user.TagList;
import com.chinaums.xm.wechat.domain.wechat.user.Tags;
import com.chinaums.xm.wechat.domain.wechat.user.UserInfo;
import com.chinaums.xm.wechat.domain.wechat.user.UserList;
import com.chinaums.xm.wechat.service.wechat.UserService;

import io.swagger.annotations.ApiOperation;

/**
 * 微信用户管理
 * @author chenjf
 *
 */
@RequestMapping("/wechat")
@RestController
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(WeixinController.class);
	
	@Resource
	private UserService userService;
	
	@ApiOperation(value = "查询用户列表", notes = "查询用户列表")
	@RequestMapping(value = { "/queryUser" }, method = RequestMethod.GET)
	public void queryUser(){
		final Optional<UserList> userListOpt =  userService.queryUser();
		if(userListOpt.isPresent()){
			logger.info("用户总数量:{}",userListOpt.get().getTotal());
			logger.info("用户数量:{}",userListOpt.get().getCount());
			final String[] openids = userListOpt.get().getData().getOpenid();
			for (String openid : openids) {
				logger.info("用户OPENID:{}",openid);
			}
		}
	}
	
	@ApiOperation(value = "查询用户个人信息", notes = "查询用户个人信息")
	@RequestMapping(value = { "/queryUserInfo" }, method = RequestMethod.GET)
	public void queryUserInfo(){
		final Optional<UserInfo> userInfoOpt = userService.getUserInfo("oImUHwPf2eYd26LtT3vwFGwqJD8k");
		if(userInfoOpt.isPresent()){
			final UserInfo userInfo = userInfoOpt.get();
			logger.info("用户信息:{}",userInfo);
		}
	}
	@ApiOperation(value = "更新用户备注", notes = "更新用户备注")
	@RequestMapping(value = { "/updateUserRemark" }, method = RequestMethod.GET)
	public void updateUserRemark(){
		final boolean result = userService.updateUserRemark("oImUHwPf2eYd26LtT3vwFGwqJD8k", "晨宝");
		logger.info("更新结果:{}",result);
	}
	
	@ApiOperation(value = "创建用户标签", notes = "创建用户标签")
	@RequestMapping(value = { "/createTag" }, method = RequestMethod.GET)
	public void createTag(){
		final Optional<Tags> tagOpt = userService.createTag("测试标签1");
		if(tagOpt.isPresent()){
			final Tags tags = tagOpt.get();
			logger.info("id:{},name:{}",tags.getTag().getId(),tags.getTag().getName());
		}
	}
	
	@ApiOperation(value = "查询用户标签", notes = "查询用户标签")
	@RequestMapping(value = { "/queryTag" }, method = RequestMethod.GET)
	public void queryTag(){
		final Optional<TagList> tagOpt = userService.queryTag();
		if(tagOpt.isPresent()){
			logger.info("{}",tagOpt.get().getTags());
		}
	}
	
	@ApiOperation(value = "更新用户标签", notes = "更新用户标签")
	@RequestMapping(value = { "/updateTag" }, method = RequestMethod.GET)
	public void updateTag(){
		final boolean result = userService.updateTag("101", "测试");
		logger.info("更新结果:{}",result);
	}
	
	@ApiOperation(value = "删除用户标签", notes = "删除用户标签")
	@RequestMapping(value = { "/deleteTag" }, method = RequestMethod.GET)
	public void deleteTag(){
		final boolean result = userService.deleteTag("101");
		logger.info("删除结果:{}",result);
	}
	
	@ApiOperation(value = "批量为用户打标签", notes = "批量为用户打标签")
	@RequestMapping(value = { "/batchTag" }, method = RequestMethod.GET)
	public void batchTag(){
		List<String> list = new ArrayList<>();
		list.add("oImUHwPf2eYd26LtT3vwFGwqJD8k");
		final boolean result = userService.tagForUser(list );
		logger.info("结果:{}",result);
	}
	
	@ApiOperation(value = "批量删除用户标签", notes = "批量删除用户标签")
	@RequestMapping(value = { "/batchDelTag" }, method = RequestMethod.GET)
	public void batchDeleteTag(){
		List<String> list = new ArrayList<>();
		list.add("oImUHwPf2eYd26LtT3vwFGwqJD8k");
		final boolean result = userService.batchDelTag(list );
		logger.info("结果:{}",result);
	}
}
